/**
 * @param {number} x
 * @return {boolean}
 */
// (2**5)(5)(7**2)(11)
function a(c) {
  var r = /^[+-]?[1-9]?[0-9]*\.[0-9]*$/;
  return r.test(c);
}
function primeFactors(n) {
  var res = []
  var flag = 2;
  let up = 0
  while (1) {
    if (a(n / flag)) {
      console.log(flag, up)
      if (up != 0) {
        if (up == 1) {
          let str = '(' + flag + '' + ')'
          res.push(str)
        } else {
          let str = '(' + flag + '**' + up + ')'
          res.push(str)
        }
      }
      up = 0
      flag++
      continue
    }
    n = n / flag
    up++
    console.log('__flag__', flag, '__uper__', up, '__n__', n)
    if (n == 1) {
      if (up == 1) {
        let str = '(' + flag + '' + ')'
        res.push(str)
      } else {
        let str = '(' + flag + '**' + up + ')'
        res.push(str)
      }
      return res.join('')
    }
  }
}

function primeFactors(n) {
  for (var i = 2, res = "", f; i <= n; i++) {
    f = 0;
    while (n % i == 0) { f++; n /= i }
    res += f ? "(" + (f > 1 ? i + "**" + f : i) + ")" : ""
  }
  return res || "(" + n + ")"
}

// --------------------------------------------------------------------------------------

//  "(p1**n1)(p2**n2)...(pk**nk)"
// with the p(i) in increasing order and n(i) empty if n(i) is 1.

// Example: n = 86240 should return "(2**5)(5)(7**2)(11)"

///// isPrime ////////////////////////////////////////////////////////////////////////
// need to make function that will return ture is num is prime
// then create function that output array which contains prime number less than num
// for ex
// if num is 86240
// store prime num until 86240
//////////////////////////////////////////////////////////////////////////////////// 

function isPrime(num) {

  if (num === 2 || num === 3) {
    return true;
  }

  if (num < 2 || num % 2 === 0) {
    return false;
  }

  for (var i = 3; i <= Math.sqrt(num); i += 2) {

    if (num % i === 0) {
      return false;
    }

  }

  return true;

}

//// funciton makePrimeArr /////////////////////////////////////////
// input 
// bigNum

// output
// arr containing prime

function getAllFactorsAndSortArr(num) {

  var arr = [];

  for (var i = 0; i <= Math.sqrt(num); i++) {
    if (num % i === 0) {
      arr.push(i);
      arr.push(num / i);
    }
  }

  var sortedArr = arr.sort(function (a, b) { return a - b; });
  return sortedArr.filter(function (val, idx, arr) {
    if (arr.indexOf(val) === idx) {
      return true;
    }
  })

}


//// main Funtion ///////////////////////////////////////////////////////
// primeArr will start from primeArr = [2]
// i will store with the loop going from 3 till 86240
// so ex for i = 3 ; i < = 86240; i++;



// now I have prime arr

// then I am going to go thorugh 
// format of the output is 
////////////////////////////////////////////////////////////////////////
function primeFactors(n) {
  var str = "";
  var allFactors = getAllFactorsAndSortArr(n);

  var primeOnlyArr = allFactors.filter(function (val) {

    return isPrime(val);

  })



  for (var i = 0; i < primeOnlyArr.length; i++) {
    var primeNum = primeOnlyArr[i];

    str += howManyPowCanYouGoUpTo(primeNum, n)
  }

  return str;
}


//// function howManyPowCanYouGoUpTo(n, num) ///////////////////////////
// need to write how many can power can you go up to ? with prime
// Math.prime(2,1)
// Math.prime(2,2)
// Math.prime(2,3)
// Math.prime(2,4)
// Math.prime(2,5)
// Math.prime(2,6) must have failed so 
// when failed then return (2.toString() +  "**" + (i-1).toString())

// if fail in (n,2)
// then output (n.toString())
////////////////////////////////////////////////////////////////////////
function howManyPowCanYouGoUpTo(n, num) {

  var p = 1
  while (num % Math.pow(n, p) === 0) {
    p++
  }
  p--

  if (p === 1) {
    return putInParentesisToStr(n.toString());
  } else {
    return putInParentesisToStr(n.toString() + "**" + p.toString());
  }

}

//// function put in parentesis (str) //////////////////////////////////
// function put str in parentesis
////////////////////////////////////////////////////////////
function putInParentesisToStr(str) {
  return "(" + str + ")";
}
// -------------------------------------------------------------------------------


function primeFactors(n) {
  for (var s = '', d = 2; n > 1; d++) {
    for (var k = 0; n % d == 0; k++, n /= d);
    s += k ? (k == 1 ? `(${d})` : `(${d}**${k})`) : '';
  }
  return s
}



const primeFactors = n => {
  if (n < 2) return `(${n})`;
  let factors = '';
  for (let i = 2; i <= n; i++) {
    let count = 0;
    while (n % i === 0) {
      count++;
      n /= i;
    }
    if (count) {
      factors += `(${i}`;
      if (count > 1) factors += `**${count}`;
      factors += `)`;
    }
  }
  return factors;
}



function primeFactors(n) {
  const fct = factors(n);
  const uniq = [...new Set(fct)];
  return uniq.map(f => {
    return `(${f}**${fct.filter(fct => fct === f).length})`.replace('**1', '');
  }).join``;
}

function factors(n) {
  let max = Math.floor(Math.sqrt(n));
  let res = [];
  for (let i = 2; i <= max; ++i) {
    if (n % i === 0) {
      res.push(i);
      n /= i;
      max = Math.floor(Math.sqrt(n));
      i = (Math.min(...res) || 2) - 1;
    }
  }
  res.push(n);
  return res;
}



const primeFactors = n => {
  const obj = {}; let i = 2;
  while (n > 1) !(n % i) ? (obj[i] = -~obj[i], n /= i) : i++;
  return Object.keys(obj).reduce((pre, val) => pre + `(${val}${obj[val] > 1 ? `**${obj[val]}` : ``})`, ``)
}


const primeFactors = n => {
  const factors = new Map();
  const incFactor = n => (count = factors.get(n) || 0, factors.set(n, count + 1));

  let rem = n;

  const handleFactor = n => {
    while (rem % n == 0) {
      incFactor(n);
      rem /= n;
    }
  };

  handleFactor(2);

  for (let i = 3; i <= rem; i += 2) {
    handleFactor(i);
  }

  return [...factors.entries()]
    .map(([prime, count]) => `(${prime}${count > 1 ? '**' + count : ''})`)
    .join('');
}



function factors(n, t = 5) {
  if (n <= 3) return [n];
  if (n % 2 === 0) return [2].concat(factors(n / 2));
  if (n % 3 === 0) return [3].concat(factors(n / 3));
  for (var i = t; i * i <= n; i += 4) {
    if (n % i === 0) return [i].concat(factors(n / i, i));
    i += 2;
    if (n % i === 0) return [i].concat(factors(n / i, i - 2));
  }
  return [n];
}

const primeFactors = n =>
  Array.from(factors(n).reduce((acc, v) => acc.set(v, acc.get(v) + 1 || 1), new Map()))
    .map(([prime, count]) => count - 1 ? `(${prime}**${count})` : `(${prime})`)
    .join("")
  ;



function primeFactors(n) {
  const factors = {};
  let str = '';
  for (let factor = 2; factor <= n;) {
    if (n % factor == 0) {
      factors[factor] = (factors[factor]) ? factors[factor] + 1 : 1;
      n /= factor;
      if (n % factor) {
        str += `(${factor}${(factors[factor] > 1) ? `**${factors[factor]}` : ''})`;
      }
    } else {
      factor++;
    }
  }
  return str;
}





function primeFactors(remainder) {
  var factors = [], i;

  for (i = 2; i <= remainder; i++) {
    while ((remainder % i) === 0) {
      factors.push(i);
      remainder /= i;
    }
  }

  return [...new Set(factors)].map(v => {
    let num = factors.filter(e => e === v).length;
    if (num > 1) {
      return `(${v}**${num})`
    }

    return `(${v})`
  }).join('')
}







function primeFactors(n) {
  let freq = {};
  for (let i = 2; n > 1; i++) if (n % i === 0) { while (n % i === 0) { freq[i] = (freq[i] || 0) + 1; n /= i; } }
  return Object.keys(freq).sort((a, b) => a - b).map(k => `(${k}${freq[k] > 1 ? '**' + freq[k] : ''})`).join('');
}

























console.log(primeFactors(86240))